	import java.awt.Font;
	import java.awt.Rectangle;
	import java.awt.event.ActionEvent;
	import java.awt.event.ActionListener;
	import javax.swing.JButton;
import javax.swing.JComboBox;
	import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 *Gui of add new Subjet window for the user's use
 */
	public class AddSubjectGui implements ActionListener 
	{
		private JLabel AddSubject = null;
		private JLabel SubjectName = null;
		private JLabel FieldName = null;
		public JTextField SubjectText = null;	
		public JComboBox FieldCombo = null;
		public JComboBox FieldCombo2 = null;
		private JButton addSub = null;	
		private JButton ViewFields = null;
		private JButton ClearSel = null;
		private JLabel RemoveSubject = null;
		private JLabel SubjectName2 = null;
		private JLabel SubjectName3 = null;
		public JComboBox SubjectCombo = null; 
		private JButton remove = null;	 
		private JButton cancel = null;
		private JButton logout1 = null;
		public JPanel panel=new JPanel(); 
		
		/**
		 * This is the default constructor
		 */
		public AddSubjectGui() {
			super();
			initialize();		
		}

		/**
		 * This method initializes this
		 * 
		 * @return void
		 */
	public void initialize() {		
			panel.setLayout(null);
			panel.add(getAddTitle(), null);
			panel.add(getAddTitle2(), null);
			panel.add(getsub1Title(), null);
			panel.add(getSubjectText(), null);
			getFieldCombo();		// first Field Combo
			getSubjectsCombo();		//SubjectCombo
			panel.add(getfield1Title(), null);			
			panel.add(getsub2Title(), null);
			panel.add(getsub3Title(), null);
			panel.add(getViewFields(), null);
			panel.add(getClearSelect(), null);
			panel.add(getLogout1(), null);
			panel.add(getadd(), null);
			panel.add(getremove(), null);
			panel.add(getCancel(), null);
			panel.setVisible(false);
			panel.setEnabled(false);
			
		}
	/**
	 * This method initializes The text Title :Add Subject
	 * 	
	 * @return javax.swing.JLabel	
	 */
		private JLabel getAddTitle() 
		{
			if (AddSubject == null) {
				AddSubject = new JLabel("Add Subject");
				AddSubject.setFont(new Font("David",Font.ITALIC,34));
				AddSubject.setBounds(new Rectangle(120, 40, 283, 44));
			}
			return AddSubject;
		}
				/**
	 * This method initializes The text instruction1 : "Enter Subject :" 
	 * 	
	 * @return javax.swing.JLabel	
	 */
		private JLabel getsub1Title() 
		{
			if (SubjectName == null) {
				SubjectName = new JLabel("Enter Subject :");
				SubjectName.setFont(new Font("David",Font.ITALIC,16));
				SubjectName.setBounds(new Rectangle(20, 80, 150, 44));
			}
			return SubjectName;
		}		
		
		JTextField getSubjectText() {
			if (SubjectText == null) {
				SubjectText = new JTextField();
				SubjectText.setBounds(new Rectangle(130, 85, 100, 36));
				SubjectText.addKeyListener(new java.awt.event.KeyAdapter() {
					public void keyTyped(java.awt.event.KeyEvent e) 
		             {
		                 Character c=e.getKeyChar();
		                 if( c.charValue()==':' || c.charValue()=='*' || c.charValue()=='-' )
		                 {
		                     e.consume();
		                     //Toolkit.getDefaultToolkit().beep();
		                     JOptionPane.showMessageDialog(null,"Invalid key !!!!","ERROR",JOptionPane.ERROR_MESSAGE);
		                     return;
		                 }
		             }	
				});
			}
			return SubjectText;
		}
							/**
	 * This method initializes The text instruction2 : "Select Field :" 
	 * 	
	 * @return javax.swing.JLabel	
	 */		
		private JLabel getfield1Title() 
		{
			if (FieldName == null) {
				FieldName = new JLabel("Select Field :");
				FieldName.setFont(new Font("David",Font.ITALIC,16));
				FieldName.setBounds(new Rectangle(20, 140, 150, 44));
		}
			return FieldName;
		}
	 /** This method initializes The FieldCombo Box with Fields retreived from DB
	 * 	
	 * @return javax.swing.JComboBox	
	 */		
		private void getFieldCombo() 
		{
			Main.bookManagementCTRL.AddSubjectCTRL.o_GetFields(this,1);	
		}
		
 /** This method initializes The Add Button and set listener on this button	
	 * 	
	 * @return javax.swing.JComboBox	
	 */	
		private JButton getadd() 
		{
			if (addSub == null) {
				addSub = new JButton("Add Subject");
				addSub.setBounds(new Rectangle(250, 80, 120, 100));
				addSub.addActionListener(this);			
			}
			return addSub;
		}		
			 /** This method initializes The text Title : "Remove Subject" 
	 * 	
	 * @return javax.swing.JLabel	
	 */	
		private JLabel getAddTitle2() 
		{
			if (RemoveSubject == null) {
				RemoveSubject = new JLabel("Remove Subject");
				RemoveSubject.setFont(new Font("David",Font.ITALIC,36));
				RemoveSubject.setBounds(new Rectangle(120, 210, 283, 44));
			}
			return RemoveSubject;
		}
		
		private JLabel getsub2Title() 
		{
			if (SubjectName2 == null) {
				SubjectName2 = new JLabel("Select Field :");
				SubjectName2.setFont(new Font("David",Font.ITALIC,16));
				SubjectName2.setBounds(new Rectangle(20, 290, 150, 44));
				SubjectName2.setVisible(false);
			}
			return SubjectName2;
		}
		
		private void getSubjectsCombo() 
		{
			Main.bookManagementCTRL.AddSubjectCTRL.o_GetSubjects(this);
		}		
		
		private JButton getViewFields() 
		{
			if (ViewFields == null) {
				ViewFields = new JButton("View related Fields");
				ViewFields.setBounds(new Rectangle(250, 250, 150, 36));
				ViewFields.addActionListener(this);				
			}
			return ViewFields;
		}	
		
		private JLabel getsub3Title() 
		{
			if (SubjectName3 == null) {
				SubjectName3 = new JLabel("Select Subject :");
				SubjectName3.setFont(new Font("David",Font.ITALIC,16));
				SubjectName3.setBounds(new Rectangle(20, 250, 150, 44));
			}
			return SubjectName3;
		}
		
		private JButton getClearSelect() 
		{
			if (ClearSel == null) {
				ClearSel = new JButton("Clear Selection");
				ClearSel.setBounds(new Rectangle(250, 290, 120, 36));
				ClearSel.addActionListener(this);	
				ClearSel.setVisible(false);
			}
			return ClearSel;
		}
		
			
		private JButton getremove() 
		{
			if (remove == null) {
				remove = new JButton("Remove Subject");
				remove.setBounds(new Rectangle(150, 350, 130, 60));
				remove.addActionListener(this);	
				remove.setEnabled(false);
			}
			return remove;
		}				
		
		private JButton getCancel() {
			if (cancel == null) {
				cancel = new JButton("Back");
				cancel.setBounds(new Rectangle(318, 8, 74, 24));
				cancel.addActionListener(this);
			}
			return cancel;
		}
		/**
		 * This method initializes logout1	
		 * 	
		 * @return javax.swing.JButton	
		 */
		JButton getLogout1() {
			if (logout1 == null) {
				logout1 = new JButton("Logout");
				logout1.setBounds(new Rectangle(402, 8, 74, 24));
				logout1.addActionListener(this);
			}
			return logout1;
		}


		public void actionPerformed(ActionEvent ae)
		{		
			if(ae.getSource()==addSub)
				Main.bookManagementCTRL.AddSubjectCTRL.o_AddSubject(this);
			if(ae.getSource()==ViewFields)
			{	
				if(((String)SubjectCombo.getSelectedItem()).equals("General"))
		    	{
		    		 JOptionPane.showMessageDialog(null,"Cannot Remove 'General' Subject!  \nEvery Field must have it","Input error",JOptionPane.ERROR_MESSAGE);
		    		 return;
		    	}
				else
				{
					ViewFields.setEnabled(false);
					SubjectCombo.setEnabled(false);
					FieldCombo2=null;
					Main.bookManagementCTRL.AddSubjectCTRL.o_GetFields(this,2);
					remove.setEnabled(true);
					ClearSel.setVisible(true);
					FieldCombo2.setVisible(true);
					FieldCombo2.updateUI();
					SubjectName2.setVisible(true);
				}
			}
			
			if(ae.getSource()==ClearSel)
			{
				ViewFields.setEnabled(true);
				SubjectCombo.setEnabled(true);
				remove.setEnabled(false);
				FieldCombo2.setVisible(false);
				panel.remove(FieldCombo2);	
				ClearSel.setVisible(false);
				SubjectName2.setVisible(false);
			}
			
			if(ae.getSource()==remove)
			{
				if(JOptionPane.showConfirmDialog(null, "Are you sure you want to remove that subject? ","Confirmation" ,JOptionPane.OK_CANCEL_OPTION) == 0)
						Main.bookManagementCTRL.AddSubjectCTRL.o_RemoveSubject(this);				
			}
				
			
			if(ae.getSource()==cancel)			
				Main.bookManagementCTRL.AddSubjectCTRL.o_Back(this.panel);
			
			if(ae.getSource()==getLogout1())				
				Main.bookManagementCTRL.o_logout(this.panel);
			
					
		}

	}
